package cn.tworice.client.service.impl;

import cn.tworice.admin.dao.CompaniesDao;
import cn.tworice.client.dao.InfoEditDao;
import cn.tworice.client.service.InfoEditService;
import cn.tworice.common.entity.Companies;
import cn.tworice.common.entity.ResumeReceived;
import cn.tworice.common.entity.Resumes;
import cn.tworice.common.entity.ShowPostsLog;
import cn.tworice.common.entity.dto.CompaniesDto;
import cn.tworice.common.entity.dto.PostsDto;
import cn.tworice.common.entity.dto.ResumeReceivedDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.net.URLDecoder;
import java.util.List;
import java.util.UUID;

@Service
public class InfoEditServiceImpl implements InfoEditService {


    @Autowired
    private CompaniesDao companiesDao;

    @Autowired
    private InfoEditDao infoEditDao;

    /**
     * 更新或设置 企业信息
     * @Author 二饭
     * @Email 1786705138@qq.com
     * @Date 2021/12/14 19:50
     */
    @Override
    public void setCompanies(Companies companies) {
        if (companies.getId() == null || companies.getId().isEmpty()) {
            companies.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            companies.setDate(System.currentTimeMillis());
            companiesDao.insertCompanies(companies);
        }else {
            companiesDao.updateCompanies(companies);
        }
    }

    @Override
    public CompaniesDto getCompaniesById(String id) {
        return companiesDao.queryCompaniesById(id);
    }

    /**
     * 添加或更新一个浏览职位记录
     * @Author 二饭
     * @Email 1786705138@qq.com
     * @Date 2021/12/15 12:08
     */
    @Override
    public void setShowPostsLog(ShowPostsLog showPostsLog) {
        showPostsLog.setDate(System.currentTimeMillis());
        ShowPostsLog showPostsLog1 = infoEditDao.queryShowPostsLogByLog(showPostsLog.getStudentId(), showPostsLog.getPostsId());
        if (showPostsLog1 == null) {
            showPostsLog.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            infoEditDao.insertShowPostsLog(showPostsLog);
        }else {
            showPostsLog.setId(showPostsLog1.getId());
            infoEditDao.updateShowPostsLog(showPostsLog);
        }
    }

    /**
     * 通过学生ID查询学生浏览过的职位
     * @Author 二饭
     * @Email 1786705138@qq.com
     * @Date 2021/12/15 12:20
     */
    @Override
    public List<PostsDto> getShowPostsLog(String studentId) {
        return infoEditDao.queryPostsByLog(studentId);
    }

    /**
     * 保存pdf文件
     * @Author 二饭
     * @Email 1786705138@qq.com
     * @Date 2021/12/15 15:33
     */
    @Override
    public String saveFile(MultipartFile file) {
        // 如果file是空
        if(file==null){
            return null;
        }
        String filePath = this.getClass().getResource("/").getPath()+"/static/";
        String newName = "";
        try {
            filePath = URLDecoder.decode(filePath, "UTF-8");
            newName=System.currentTimeMillis()+"";
            file.transferTo(new File(filePath+newName+".pdf"));
        }catch (Exception ex){
            ex.printStackTrace();
        }

        return newName;
    }

    @Override
    public void setResumes(String studentId, String name) {
        Resumes resumes = infoEditDao.queryResumesByStudentId(studentId);
        if (resumes == null || resumes.getId() == null) {
            resumes = new Resumes();
            resumes.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            resumes.setResumesFile(name);
            resumes.setDate(System.currentTimeMillis());
            resumes.setStudentId(studentId);
            infoEditDao.insertResumes(resumes);
        }else{
            resumes.setResumesFile(name);
            resumes.setDate(System.currentTimeMillis());
            infoEditDao.updateResumes(resumes);
        }
    }

    @Override
    public String getResumesByStudent(String studentId) {
        Resumes resumes = infoEditDao.queryResumesByStudentId(studentId);
        if(resumes==null){
            return null;
        }
        return resumes.getResumesFile();
    }

    @Override
    public String setResumeReceived(ResumeReceived resumeReceived) {
        // 通过学生Id查询该学生的简历
        Resumes resumes = infoEditDao.queryResumesByStudentId(resumeReceived.getStudentId());
        if(resumes==null){
            return "您没有简历可以投递";
        }else{
            // 通过职位ID和学生Id 查询到该学生是否投递过简历
            ResumeReceived resumeReceived1=infoEditDao.queryResumeReceivedByInfo(resumeReceived.getPostsId(), resumeReceived.getStudentId());
            if(resumeReceived1==null){
                // 没有投递过
                resumeReceived.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                resumeReceived.setDate(System.currentTimeMillis());
                infoEditDao.insertResumeReceived(resumeReceived);
            }else{
                return "不能重复投递简历";
            }
        }
        return "简历已投递";
    }

    @Override
    public List<ResumeReceivedDto> getResumeReceivedByStudentId(String studentId,Integer page,Integer pageSize) {
        return infoEditDao.queryResumeReceivedByStudentId(studentId,(page*pageSize),pageSize);
    }

    @Override
    public Integer getResumeReceivedTotalByStudentId(String studentId) {
        return infoEditDao.queryResumeReceivedTotalByStudentId(studentId);
    }

    @Override
    public void delResumeReceived(String id) {
        infoEditDao.delResumeReceived(id);
    }


}
